/*
   Example 8.2
   http://tronixstuff.com/tutorials > Chapter 8
   Receive an integer from serial input and work with it
 */

int number = 0;
int a = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  number=0;
  while (Serial.available()==0)
  {
    // do nothing until something comes into the serial buffer
  } 
  while (Serial.available()>0)
  {
    while (Serial.available()>0)
    {
      number=number*10; // move the previous digit to the next column on the left, e.g. 1 becomes 10
      // while there is data in the buffer
      a = Serial.read()-48; // read the next number in the buffer, subtract 48 to convert to the actual number
      number=number+a;
    }
    delay(5); // the processor needs a moment to process
  }
  // Show me the number!
  Serial.print("You entered: ");
  Serial.println(number);
  Serial.print(number);
  Serial.print(" multiplied by two is ");
  number=number*2;
  Serial.println(number);
  // clear out the serial buffer ready for another turn
  Serial.flush();
}


